package com.skp.shaphan.model;

import java.sql.Connection;

import com.skp.shaphan.sql.DerbySPP;
import com.skp.shaphan.sql.MySQLSPP;
import com.skp.shaphan.sql.OracleSPP;
import com.skp.shaphan.sql.SQLPreProcessor;
import com.skp.shaphan.sql.SQLServerSPP;

/**
 * Wraps the SQL Connection and stores extra details about the connection.
 * @author Stephen Phillips
 *
 */
public class ConnectionDetails {
	/**
	 * Connection Type -- we haven't connected to anything yet.
	 * @see getConnection()
	 * @see setConnection()
	 * @see connectionType
	 */
	public static final int CONN_TYPE_NOT_CONNECTED = 0;
	/**
	 * Connection Type -- connection to a SQL Server database.
	 * @see getConnection()
	 * @see setConnection()
	 * @see connectionType
	 */
	public static final int CONN_TYPE_SQL_SERVER = 1;
	/**
	 * Connection Type -- Connection to an Apache Derby database.
	 * @see getConnection()
	 * @see setConnection()
	 * @see connectionType
	 */
	public static final int CONN_TYPE_DERBY = 2;
	/**
	 * Connection Type -- connection to an Orale database.
	 * @see getConnection()
	 * @see setConnection()
	 * @see connectionType
	 */
	public static final int CONN_TYPE_ORACLE = 3;
	/**
	 * Connection Type -- connection to a MySQL database.
	 * @see getConnection()
	 * @see setConnection()
	 * @see connectionType
	 */
	public static final int CONN_TYPE_MYSQL = 4;
	/**
	 * Connection Type -- connection to a ODBC database connection.
	 * @see getConnection()
	 * @see setConnection()
	 * @see connectionType
	 */
	public static final int CONN_TYPE_ODBC = 5;
	
	/**
	 * The JDBC Connection object
	 */
	private Connection connection = null;
	/**
	 * The type of connection
	 * @see CONN_TYPE_DERBY
	 * @see CONN_TYPE_MYSQL
	 * @see CONN_TYPE_NOT_CONNECTED
	 * @see CONN_TYPE_ODBC
	 * @see CONN_TYPE_ORACLE
	 * @see CONN_TYPE_SQL_SERVER
	 */
	private int connectionType = CONN_TYPE_NOT_CONNECTED;
	
	/**
	 * Instantiate a blank connection details object.  Not connected to anything.
	 */
	public ConnectionDetails() {
		
	}
	
	/**
	 * Instantiate a connection details object with a connection.
	 * @param connection
	 * @param connectionType
	 */
	public ConnectionDetails(Connection connection, int connectionType) {
		super();
		this.connection = connection;
		this.connectionType = connectionType;
	}

	/**
	 * Retrieves the JDBC connection object used to access the database
	 * @return
	 */
	public Connection getConnection() {
		return connection;
	}
	/**
	 * Sets the JDBC connection object used to access the database
	 * @param connection
	 */
	public void setConnection(Connection connection) {
		this.connection = connection;
	}
	/**
	 * Retrieves the type of database that the connection object gives access to
	 * @return
	 */
	public int getConnectionType() {
		return connectionType;
	}
	/**
	 * Sets the type of database that the connection object gives access to
	 * @see CONN_TYPE_DERBY
	 * @see CONN_TYPE_MYSQL
	 * @see CONN_TYPE_NOT_CONNECTED
	 * @see CONN_TYPE_ODBC
	 * @see CONN_TYPE_ORACLE
	 * @see CONN_TYPE_SQL_SERVER
	 * @param connectionType
	 */
	public void setConnectionType(int connectionType) {
		this.connectionType = connectionType;
	}
	
	/**
	 * Retrieves an instance of the SQL Pre Processor for this connection/connection type
	 * @param sqlStatement
	 * @return
	 */
	public SQLPreProcessor getSQLPreprocessor(String sqlStatement) {
		SQLPreProcessor spp = null;
		switch(connectionType) {
		case CONN_TYPE_ORACLE:
			spp = new OracleSPP(sqlStatement);
			break;
		case CONN_TYPE_MYSQL:
			spp = new MySQLSPP(sqlStatement);
			break;
		case CONN_TYPE_ODBC:
			// This needs to be fixed.  An ODBC connection could be any of the 
			// other types of databases.
			spp = new SQLPreProcessor(sqlStatement);
			break;
		case CONN_TYPE_DERBY:
			spp = new DerbySPP(sqlStatement);
			break;
		case CONN_TYPE_SQL_SERVER:
			spp = new SQLServerSPP(sqlStatement);
			break;
		default:
			spp = new SQLPreProcessor(sqlStatement);
			break;
		}
		return spp;
	}
}
